#!/usr/bin/python
# -*- coding: utf-8 -*-
from PyQt4.QtGui import QWidget,QTabWidget,QMainWindow,QAction,QKeySequence
from PyQt4 import QtCore
from KNonTrade import ui_KJobCrawlerDlg
import re
import mechanize
import urllib2

'''
warning !:  to support Chinese character, you need go to  C:\Program Files (x86)\Wing IDE 4.1\bin\gtk-bin\etc\pango , and change the font to a more complete font, such as Microsoft YaHei

'''

class KJobCrawlerDlg(QWidget):

	SymbolChanged=QtCore.pyqtSignal(str) # triggerd by smart look up
	def __init__(self, parent=None):
		super(KJobCrawlerDlg, self).__init__(parent)
		self.ui =  ui_KJobCrawlerDlg.Ui_KJobCrawlerDlg()
		self.ui.setupUi(self) 

		endate=QtCore.QDate.currentDate()
		startdate=endate.addDays(-1)

		self.ui.dateEditStart.setDate(startdate)
		self.ui.dateEditEnd.setDate(endate)

		self.ui.BtnScanRange.clicked.connect(self.OnScanBBS)


	


	def OnScanBBS(self):
		br = mechanize.Browser()
		weburl='http://www.mitbbs.com/bbsdoc/JobHunting.html'
		r = br.open(weburl)
		response=br.follow_link(url_regex='/bbsdoc1/JobHunting_*_0.html')
		print response.read()


		


	
		# Show the available forms
		for f in br.forms():
			print f

		# Select the first (index zero) form
		br.select_form(nr=0)

		# Let's search
		br.form['q']='weekend codes'
		br.submit()
		print br.response().read()

		# Looking at some results in link format
		for l in br.links(url_regex='stockrt'):
			print l		
		pagenum=1
		while True:
			weburl='http://www.mitbbs.com/bbsdoc1/JobHunting_%s_0.html'%(pagenum)
			weburl='http://www.mitbbs.com/bbsdoc/JobHunting.html'
			code= urllib2.urlopen(weburl).code
			if (code/100<=4): # url exist?
				print(pagenum)
				pagenum+=1
				#firstpage= f.read()
				#print(firstpage)
			else:
				break









